package com.fs.erp.api.basedata.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fs.erp.api.basedata.bo.product.QueryProductSalePropBo;
import com.fs.erp.api.basedata.entity.ProductCategory;
import com.fs.erp.api.basedata.entity.ProductSalepropGroup;
import com.fs.erp.api.basedata.mappers.ProductSalepropMapper;
import com.fs.erp.api.basedata.service.ProductSalepropService;
import com.fs.erp.api.basedata.vo.UpdateProductSalepropVo;
import com.fs.erp.api.basedata.vo.product.QueryProductSalePropVo;
import com.fs.erp.api.basedata.vo.product.UpdateProductPropertVo;
import com.fs.erp.common.constants.StringPool;
import com.fs.erp.common.exceptions.impl.DefaultClientException;
import com.fs.erp.common.utils.Assert;
import com.fs.erp.common.utils.StringUtil;
import com.fs.erp.mybatis.mapper.BaseMapper;
import com.fs.erp.mybatis.resp.PageResult;
import com.fs.erp.mybatis.utils.PageHelperUtil;
import com.fs.erp.mybatis.utils.PageResultUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductSalepropServiceImpl extends BaseMpServiceImpl<ProductSalepropMapper, ProductSalepropGroup>
        implements ProductSalepropService {

    private ProductSalepropMapper salepropMapper;

    @Override
    public PageResult<ProductSalepropGroup> query(Integer pageIndex, Integer pageSize, QueryProductSalePropVo vo) {
        Assert.greaterThanZero(pageIndex);
        Assert.greaterThanZero(pageSize);

        PageHelperUtil.startPage(pageIndex,pageSize);
        List<ProductSalepropGroup> salepropGroups=getBaseMapper().querySaleprop(vo);
        return PageResultUtil.convert(new PageInfo<>(salepropGroups));
    }

    /**
     * 修改
     * @param vo
     */
    @Override
    public void updateSaleprop(UpdateProductSalepropVo vo) {
          salepropMapper.updateAll(vo);
//        ProductSalepropGroup salepropGroup= getBaseMapper().selectById(vo.getId());
//
//        //code是否重复
//        Wrapper<ProductSalepropGroup> checkCodeWrapper = Wrappers.lambdaQuery(ProductSalepropGroup.class)
//                .eq(ProductSalepropGroup::getCode,vo.getCode()).ne(ProductSalepropGroup::getId,vo.getId());
//        if (getBaseMapper().selectCount(checkCodeWrapper)>0){
//            throw new DefaultClientException("编号重复");
//        }
//
//        //name是否重复
//        Wrapper<ProductSalepropGroup> checkNameWrapper = Wrappers.lambdaQuery(ProductSalepropGroup.class)
//                .eq(ProductSalepropGroup::getName,vo.getName()).ne(ProductSalepropGroup::getId,vo.getId());
//        if (getBaseMapper().selectCount(checkNameWrapper)>0){
//            throw new DefaultClientException("名称重复");
//        }
//
//        Wrapper<ProductSalepropGroup> updateWrapper= Wrappers.lambdaUpdate(ProductSalepropGroup.class)
//                .set(ProductSalepropGroup::getCode,vo.getCode()).set(ProductSalepropGroup::getName,vo.getName())
//                .set(ProductSalepropGroup::getDescription, StringUtil.isBlank(vo.getDescription())? StringPool.EMPTY_STR:vo.getDescription())
//                .set(ProductSalepropGroup::getAvailable,vo.getAvailable())
//                .eq(ProductSalepropGroup::getId,vo.getId());
//        update(updateWrapper);
    }

    @Override
    public QueryProductSalePropBo selectById(String id) {
        return getBaseMapper().selectSalepropById(id);
    }

    @Override
    public boolean update(Wrapper<ProductSalepropGroup> updateWrapper) {
        return super.update(updateWrapper);
    }
}
